home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / auxtk.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.8 KB  |  289 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /* this file contains miscellaneous tk functions ported to aux */
  39.  
  40. #include <aux.h>
  41. #include <auxPrivate.h>
  42. #include <string.h>
  43.  
  44. #if defined(__cplusplus) || defined(c_plusplus)
  45. #define class c_class
  46. #endif
  47.  
  48.  
  49. void auxCloseWindow(void)
  50. {
  51.     auxWinClose( auxState.current->id );
  52. }
  53.  
  54. Display *auxXDisplay(void)
  55. {
  56.     return (auxState.display);
  57. }
  58.  
  59. Window auxXWindow(void)
  60. {
  61.  
  62.     return (auxState.current->glxWindow);
  63. }
  64.  
  65. /* bit bucket for errors generated when setting colors */
  66. static int Ignore(Display *parm1, XErrorEvent *parm2)
  67. {
  68.     return 0;
  69. }
  70.  
  71.  
  72. /* ported from tkSetOneColor() */
  73. void auxSetOneColor(int index, float r, float g, float b)
  74. {
  75.     XErrorHandler old_handler;
  76.     XColor c;
  77.     int rShift, gShift, bShift;
  78.  
  79.     old_handler = XSetErrorHandler(Ignore);
  80.     switch (auxState.current->glxVisual->class) {
  81.       case DirectColor:
  82.     rShift = ffs((unsigned int)auxState.current->glxVisual->red_mask) - 1;
  83.     gShift = ffs((unsigned int)auxState.current->glxVisual->green_mask) - 1;
  84.     bShift = ffs((unsigned int)auxState.current->glxVisual->blue_mask) - 1;
  85.     c.pixel = ((index << rShift) & auxState.current->glxVisual->red_mask) |
  86.           ((index << gShift) & auxState.current->glxVisual->green_mask) |
  87.           ((index << bShift) & auxState.current->glxVisual->blue_mask);
  88.     c.red = (unsigned short)(r * 65535.0 + 0.5);
  89.     c.green = (unsigned short)(g * 65535.0 + 0.5);
  90.     c.blue = (unsigned short)(b * 65535.0 + 0.5);
  91.     c.flags = DoRed | DoGreen | DoBlue;
  92.     XStoreColor(auxState.display, auxState.current->colormap, &c);
  93.     break;
  94.       case GrayScale:
  95.       case PseudoColor:
  96.     if (index < auxState.current->glxVisual->colormap_size) {
  97.         c.pixel = index;
  98.         c.red = (unsigned short)(r * 65535.0 + 0.5);
  99.         c.green = (unsigned short)(g * 65535.0 + 0.5);
  100.         c.blue = (unsigned short)(b * 65535.0 + 0.5);
  101.         c.flags = DoRed | DoGreen | DoBlue;
  102.         XStoreColor(auxState.display, auxState.current->colormap, &c);
  103.     }
  104.     break;
  105.     }
  106.  
  107.     XSync(auxState.display, 0);
  108.     XSetErrorHandler(old_handler);
  109. }
  110.  
  111. void auxSetFogRamp(int density, int startIndex)
  112. {
  113.     XErrorHandler old_handler;
  114.     XColor c[4096];
  115.     int rShift, gShift, bShift, intensity, fogValues, colorValues;
  116.     int i, j, k;
  117.  
  118.     old_handler = XSetErrorHandler(Ignore);
  119.  
  120.     switch (auxState.current->glxVisual->class) {
  121.       case DirectColor:
  122.     fogValues = 1 << density;
  123.     colorValues = 1 << startIndex;
  124.     for (i = 0; i < colorValues; i++) {
  125.         for (j = 0; j < fogValues; j++) {
  126.         k = i * fogValues + j;
  127.         intensity = i * fogValues + j * colorValues;
  128.         if (intensity > auxState.current->glxVisual->colormap_size) {
  129.             intensity = auxState.current->glxVisual->colormap_size;
  130.         }
  131.         intensity = (intensity << 8) | intensity;
  132.         rShift = ffs((unsigned int)auxState.current->glxVisual->red_mask) - 1;
  133.         gShift = ffs((unsigned int)auxState.current->glxVisual->green_mask) - 1;
  134.         bShift = ffs((unsigned int)auxState.current->glxVisual->blue_mask) - 1;
  135.         c[k].pixel = ((k << rShift) & auxState.current->glxVisual->red_mask) |
  136.                  ((k << gShift) & auxState.current->glxVisual->green_mask) |
  137.                  ((k << bShift) & auxState.current->glxVisual->blue_mask);
  138.         c[k].red = (unsigned short)intensity;
  139.         c[k].green = (unsigned short)intensity;
  140.         c[k].blue = (unsigned short)intensity;
  141.         c[k].flags = DoRed | DoGreen | DoBlue;
  142.         }
  143.     }
  144.     XStoreColors(auxState.display, auxState.current->colormap, c,
  145.         auxState.current->glxVisual->colormap_size);
  146.     break;
  147.       case GrayScale:
  148.       case PseudoColor:
  149.     fogValues = 1 << density;
  150.     colorValues = 1 << startIndex;
  151.     for (i = 0; i < colorValues; i++) {
  152.         for (j = 0; j < fogValues; j++) {
  153.         k = i * fogValues + j;
  154.         intensity = i * fogValues + j * colorValues;
  155.         if (intensity > auxState.current->glxVisual->colormap_size) {
  156.             intensity = auxState.current->glxVisual->colormap_size;
  157.         }
  158.         intensity = (intensity << 8) | intensity;
  159.         c[k].pixel = k;
  160.         c[k].red = (unsigned short)intensity;
  161.         c[k].green = (unsigned short)intensity;
  162.         c[k].blue = (unsigned short)intensity;
  163.         c[k].flags = DoRed | DoGreen | DoBlue;
  164.         }
  165.     }
  166.     XStoreColors(auxState.display, auxState.current->colormap, c,
  167.         auxState.current->glxVisual->colormap_size);
  168.     break;
  169.     }
  170.  
  171.     XSync(auxState.display, 0);
  172.     XSetErrorHandler(old_handler);
  173. }
  174.  
  175. void auxSetGreyRamp(void)
  176. {
  177.     XErrorHandler old_handler;
  178.     XColor c[4096];
  179.     float intensity;
  180.     int rShift, gShift, bShift, i;
  181.  
  182.     old_handler = XSetErrorHandler(Ignore);
  183.  
  184.     switch (auxState.current->glxVisual->class) {
  185.       case DirectColor:
  186.     for (i = 0; i < auxState.current->glxVisual->colormap_size; i++) {
  187.         intensity = (float)i / (float)auxState.current->glxVisual->colormap_size *
  188.             65535.0 + 0.5;
  189.         rShift = ffs((unsigned int)auxState.current->glxVisual->red_mask) - 1;
  190.         gShift = ffs((unsigned int)auxState.current->glxVisual->green_mask) - 1;
  191.         bShift = ffs((unsigned int)auxState.current->glxVisual->blue_mask) - 1;
  192.         c[i].pixel = ((i << rShift) & auxState.current->glxVisual->red_mask) |
  193.              ((i << gShift) & auxState.current->glxVisual->green_mask) |
  194.              ((i << bShift) & auxState.current->glxVisual->blue_mask);
  195.         c[i].red = (unsigned short)intensity;
  196.         c[i].green = (unsigned short)intensity;
  197.         c[i].blue = (unsigned short)intensity;
  198.         c[i].flags = DoRed | DoGreen | DoBlue;
  199.     }
  200.     XStoreColors(auxState.display, auxState.current->colormap, c, auxState.current->glxVisual->colormap_size);
  201.     break;
  202.       case GrayScale:
  203.       case PseudoColor:
  204.     for (i = 0; i < auxState.current->glxVisual->colormap_size; i++) {
  205.         intensity = (float)i / (float)auxState.current->glxVisual->colormap_size *
  206.             65535.0 + 0.5;
  207.         c[i].pixel = i;
  208.         c[i].red = (unsigned short)intensity;
  209.         c[i].green = (unsigned short)intensity;
  210.         c[i].blue = (unsigned short)intensity;
  211.         c[i].flags = DoRed | DoGreen | DoBlue;
  212.     }
  213.     XStoreColors(auxState.display, auxState.current->colormap, c, auxState.current->glxVisual->colormap_size);
  214.     break;
  215.     }
  216.  
  217.     XSync(auxState.display, 0);
  218.     XSetErrorHandler(old_handler);
  219. }
  220.  
  221. void auxSetRGBMap(int size, float *rgb)
  222. {
  223.     XErrorHandler old_handler;
  224.     XColor c;
  225.     int rShift, gShift, bShift, max, i;
  226.  
  227.     old_handler = XSetErrorHandler(Ignore);
  228.  
  229.     switch (auxState.current->glxVisual->class) {
  230.       case DirectColor:
  231.     max = (size > auxState.current->glxVisual->colormap_size) ? 
  232.         auxState.current->glxVisual->colormap_size : size;
  233.     for (i = 0; i < max; i++) {
  234.         rShift = ffs((unsigned int)auxState.current->glxVisual->red_mask) - 1;
  235.         gShift = ffs((unsigned int)auxState.current->glxVisual->green_mask) - 1;
  236.         bShift = ffs((unsigned int)auxState.current->glxVisual->blue_mask) - 1;
  237.         c.pixel = ((i << rShift) & auxState.current->glxVisual->red_mask) |
  238.               ((i << gShift) & auxState.current->glxVisual->green_mask) |
  239.               ((i << bShift) & auxState.current->glxVisual->blue_mask);
  240.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  241.         c.green = (unsigned short)(rgb[size+i] *
  242.                        65535.0 + 0.5);
  243.         c.blue = (unsigned short)(rgb[size*2+i] *
  244.                       65535.0 + 0.5);
  245.         c.flags = DoRed | DoGreen | DoBlue;
  246.         XStoreColor(auxState.display, auxState.current->colormap, &c);
  247.     }
  248.     break;
  249.       case GrayScale:
  250.       case PseudoColor:
  251.     max = (size > auxState.current->glxVisual->colormap_size) ? 
  252.         auxState.current->glxVisual->colormap_size : size;
  253.     for (i = 0; i < max; i++) {
  254.         c.pixel = i;
  255.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  256.         c.green = (unsigned short)(rgb[size+i] *
  257.                        65535.0 + 0.5);
  258.         c.blue = (unsigned short)(rgb[size*2+i] *
  259.                       65535.0 + 0.5);
  260.         c.flags = DoRed | DoGreen | DoBlue;
  261.         XStoreColor(auxState.display, auxState.current->colormap, &c);
  262.     }
  263.     break;
  264.     }
  265.  
  266.     XSync(auxState.display, 0);
  267.     XSetErrorHandler(old_handler);
  268. }
  269.  
  270. GLint auxGetColorMapSize(void)
  271. {
  272.     if (!auxState.display) {
  273.     return 0;
  274.     } else {
  275.     return auxState.current->glxVisual->colormap_size;
  276.     }
  277. }
  278.  
  279. void auxGetMouseLoc(int *x, int *y)
  280. {
  281.     int junk;
  282.  
  283.     *x = 0;
  284.     *y = 0;
  285.     XQueryPointer(auxState.display, auxState.current->glxWindow, 
  286.     (Window *)&junk, (Window *)&junk,
  287.     &junk, &junk, x, y, (unsigned int *)&junk);
  288. }
  289.